package com.charlotte.study.gupao.proxy.dynamicproxy.cglibproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib的动态代理实现
 * @author junjie.ding
 * @date 2020/3/3
 */
public class CglibMeipo implements MethodInterceptor {

    public Object getInstance(Class<?> clazz){
        Enhancer enhancer = new Enhancer();
        // 将传入的实际类声明为代理类的父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object invoke = methodProxy.invoke(o, objects);
        after();
        return invoke;
    }

    private void before(){
        System.out.println("交给媒婆");
    }

    private void after(){
        System.out.println("媒婆根据条件来筛选");
    }

}
